package com.xmy.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.study.entity.EduTask;
import com.xmy.study.mapper.EduTaskMapper;
import com.xmy.study.service.IEduTaskService;
import com.xmy.study.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class EduTaskServiceImpl extends ServiceImpl<EduTaskMapper, EduTask> implements IEduTaskService {

    @Autowired
    EduTaskMapper eduTaskMapper;

    @Override
    public EduTask getTaskOneForDate(String startDate, String endDate, String yearClassId, Long classTypeId, Long courseVersionId, Long subjectsId, Integer categoryId){
        return eduTaskMapper.getTaskOneForDate(startDate,endDate,yearClassId,classTypeId,courseVersionId,subjectsId,categoryId);
    }

    @Override
    public Integer getTaskNumForMax(String yearClassId, Long classTypeId,Long courseVersionId,Long subjectsId,Integer categoryId){
        return eduTaskMapper.getTaskNumForMax(yearClassId,classTypeId,courseVersionId,subjectsId,categoryId);
    }



    @Override
    public EduTask getTaskOneForTaskCourseNum(Integer taskCourseNum, Integer yearClassId, Long classTypeId,Long courseVersionId,Long subjectsId,Integer yearPart,Integer quarterNum,Integer categoryId){
        return eduTaskMapper.getTaskOneForTaskCourseNum(taskCourseNum,yearClassId,classTypeId,courseVersionId,subjectsId,yearPart,quarterNum,categoryId);
    }

    @Override
    public EduTask getTaskOneNow(String yearPart, String quarterNum,Long courseVersionId,String subjectsId,Integer categoryId){
        LocalDate monDay = DateUtil.getMonDay();
        LocalDate sunDay = DateUtil.getSunDay();
        EduTask eduTask = eduTaskMapper.getTaskOneNow(yearPart,quarterNum,monDay.toString(),sunDay.toString(),courseVersionId,subjectsId,categoryId);
        return eduTask;
    }

    /**
     * 返回当前时间布置的最大任务关数
     * @param yearPart
     * @param quarterNum
     * @param courseVersionId
     * @param subjectsId
     * @return
     */
    @Override
    public EduTask getTaskOneMax(String yearPart, String quarterNum, Long courseVersionId, String subjectsId, Integer categoryId){
        LocalDate monDay = DateUtil.getMonDay();
        LocalDate sunDay = DateUtil.getSunDay();
        EduTask eduTask = eduTaskMapper.getTaskOneNow(yearPart,quarterNum,monDay.toString(),sunDay.toString(),courseVersionId,subjectsId,categoryId);
        if (eduTask == null){
            eduTask = this.getTaskOneBackTaskNum(yearPart,quarterNum,courseVersionId,subjectsId,categoryId);
        }
        return eduTask;
    }

    @Override
    public EduTask getTaskOneBackTaskNum(String yearPart, String quarterNum, Long courseVersionId, String subjectsId, Integer categoryId) {
        return  eduTaskMapper.getTaskOneBackTaskNum(yearPart,quarterNum,courseVersionId,subjectsId,categoryId);
    }

    @Override
    public Integer brushCount(String yearPart, String quarterNum, Integer categoryId) {
        return eduTaskMapper.brushCount(yearPart,quarterNum,categoryId);
    }


    @Override
    public EduTask getTaskOneForLessonNum(Integer yearPart, Integer quarterNum, Integer yearClassId, Long courseVersionId, Long subjectsId, Integer categoryId, Integer lessonNum){
        QueryWrapper<EduTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_course_num",lessonNum);
        queryWrapper.eq("task_category_id",categoryId);
        queryWrapper.eq("course_version_id",courseVersionId);
        queryWrapper.eq("subjects_id",subjectsId);
        queryWrapper.eq("year_class_id",yearClassId);
        queryWrapper.eq("year_part",yearPart);
        queryWrapper.eq("quarter_num",quarterNum);
        queryWrapper.last("limit 1");
        EduTask eduTask = eduTaskMapper.selectOne(queryWrapper);
        return eduTask;
    }
}
